import {_get} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {iframe} from "@/js/tool.js";

function $path(src){
    let base = this.__iframe ? this.__iframe.path : this.$route.path;
    return src ? s94Mjs.path(src, base) : base;
}

function $get(key, def){
    return _get(key, def, this?.__iframe?.param);
}

/**判断当前组件是否在子窗口环境，如果是返回对应子窗口的app，否则返回false
 * @returns {boolean|object}
 */
function $in_iframe(){
    let max_layer = 100;
    let app = this;
    while (app && typeof app === 'object' && max_layer>0){
        if (app.__iframe && typeof app.__iframe === 'object' && app.__iframe.path) return app;
        app = app.$parent;
        max_layer--;
    }
    return false;
}

function $page_back(){
    let iframe_path = this.__iframe?.path;
    if (iframe_path){
        iframe.remove(iframe_path);
    }else {
        this.$router.back();
    }
}
function $page_back_loose(){
    let app = this.$in_iframe();
    let iframe_path = app?.__iframe?.path;
    if (iframe_path){
        iframe.remove(iframe_path);
    }else {
        this.$router.back();
    }
}

let helpers = {
    $path,
    $get,
    $in_iframe,
    $page_back,
    $page_back_loose,
}

let keys = Object.keys(helpers);
helpers.install = function (vue){
    keys.forEach((k)=>{
        vue.config.globalProperties[k] = helpers[k];
    })
}

export default helpers;